﻿using System;

namespace CL.Mobile.OnlinePay
{
    public partial class payconfirm : System.Web.UI.Page
    {
        int amount = 0;
        string orderno = string.Empty;
        CL.Game.BLL.UsersPayDetailBLL bllPay = new CL.Game.BLL.UsersPayDetailBLL();

        CL.Tools.Common.YFBH5PayHelper ybgHelper = new Tools.Common.YFBH5PayHelper();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //订单金额（单位：分）
                string payAmount = Request.QueryString["amount"];
                int.TryParse(payAmount, out amount);
                ltAmount.Text = (amount / 100).ToString("N2");
                //订单编号
                ltOrderNo.Text= orderno = Request.QueryString["orderno"];
                //监测订单是否存在                
                if (amount <= 0 || orderno.Trim() == string.Empty || (orderno.Trim() !=string.Empty && !bllPay.IsTboutTradeNo(orderno)))
                {
                    btnSubmit.Visible = false;
                    ltNotice.Text = "订单信息有误！";
                    return;
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (ltOrderNo.Text != string.Empty)
            {
                string ip = Request.UserHostAddress;
                //提交订单
               var postData = ybgHelper.BuildDoPay(orderno, ip,amount.ToString());
               string resData = CL.Tools.Common.Utils.HttpSubmit(ybgHelper.Req_Url, postData);
                ltNotice.Text = resData;
                var PayInfo = ybgHelper.GetPrePayInfo(resData);
                if (PayInfo.return_code == "0")
                {
                    string payUrl = PayInfo.pay_params.pay_info.Replace("\n","");
                    Response.Redirect(payUrl);
                }
            }
        }       

       

        protected void btnOrderQuery_Click(object sender, EventArgs e)
        {
            Response.Redirect("/PayResult.aspx?orderno=" + ltOrderNo.Text);
        }
    }
}